<?php

namespace app\models;

use app\models\user\User;
use Yii;

/**
 * This is the model class for table "year".
 *
 * @property int $id
 * @property int $user_id
 * @property int $level
 * @property int $type 1 减肥 2 面部
 * @property float $total 总计
 * @property float $children_total 需要减去的子团队业绩
 * @property int $ratio 分红比例
 * @property float $amount 分红金额
 * @property int $recommend_num 直推数量
 * @property string $create_time
 * @property string $update_time
 */
class Year extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'year';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['type', 'ratio', 'recommend_num'], 'integer'],
            [['total', 'children_total', 'amount'], 'number'],
            [['create_time', 'update_time', 'user_id', 'level'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'type' => 'Type',
            'total' => 'Total',
            'children_total' => 'Children Total',
            'ratio' => 'Ratio',
            'amount' => 'Amount',
            'recommend_num' => 'Recommend Num',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
        ];
    }

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            $time = date('Y-m-d H:i:s');
            $this->update_time = $time;
            if ($insert) {
                $this->create_time = $time;
            }
            return true;
        }
        return false;
    }

    public function getUser()
    {
        return $this->hasOne(User::class, ['id' => 'user_id']);
    }
}
